package service.Impl;

import dao.PatientDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Patient;
import pojo.Result;
import service.PatientService;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    private PatientDao patientDao;

    @Override
    public Result addPatient(Patient patient) {
        Result result = new Result();
        
        try {
            // 参数验证
            if (patient.getUserId() == null) {
                result.setCode(400);
                result.setMessage("用户ID不能为空");
                return result;
            }
            
            if (patient.getName() == null || patient.getName().trim().isEmpty()) {
                result.setCode(400);
                result.setMessage("患者姓名不能为空");
                return result;
            }
            
            if (patient.getIdCard() == null || patient.getIdCard().trim().isEmpty()) {
                result.setCode(400);
                result.setMessage("身份证号不能为空");
                return result;
            }
            
            if (patient.getSex() == null || patient.getSex().trim().isEmpty()) {
                result.setCode(400);
                result.setMessage("性别不能为空");
                return result;
            }
            
            if (patient.getBirthdate() == null || patient.getBirthdate().trim().isEmpty()) {
                result.setCode(400);
                result.setMessage("出生日期不能为空");
                return result;
            }
            
            if (patient.getTelephone() == null || patient.getTelephone().trim().isEmpty()) {
                result.setCode(400);
                result.setMessage("电话号码不能为空");
                return result;
            }
            
            // 检查身份证号是否已存在
            Patient existingPatient = patientDao.getPatientByIdCard(patient.getIdCard());
            if (existingPatient != null) {
                result.setCode(400);
                result.setMessage("该身份证号已存在");
                return result;
            }
            
            // 添加患者信息
            int rows = patientDao.addPatient(patient);
            
            if (rows > 0) {
                result.setCode(200);
                result.setMessage("添加患者信息成功");
                result.setData(patient);
            } else {
                result.setCode(500);
                result.setMessage("添加患者信息失败");
            }
            
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("系统错误：" + e.getMessage());
        }
        
        return result;
    }

    @Override
    public Result getPatientList(Integer userId) {
        Result result = new Result();
        
        try {
            if (userId == null) {
                result.setCode(400);
                result.setMessage("用户ID不能为空");
                return result;
            }
            
            List<Patient> patients = patientDao.getPatientsByUserId(userId);
            
            result.setCode(200);
            result.setMessage("获取患者列表成功");
            result.setData(patients);
            
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("系统错误：" + e.getMessage());
        }
        
        return result;
    }

    @Override
    public Result getPatientDetail(Integer patientId) {
        Result result = new Result();
        
        try {
            if (patientId == null) {
                result.setCode(400);
                result.setMessage("患者ID不能为空");
                return result;
            }
            
            Patient patient = patientDao.getPatientById(patientId);
            
            if (patient != null) {
                result.setCode(200);
                result.setMessage("获取患者详情成功");
                result.setData(patient);
            } else {
                result.setCode(404);
                result.setMessage("患者信息不存在");
            }
            
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("系统错误：" + e.getMessage());
        }
        
        return result;
    }
} 